#include  "BlockQueue.hpp"
#include "Task.hpp"
#include<unistd.h>
#include<time.h>
void* pro(void* arg)
{
  BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(arg);
  const char* str_op = "+-*/%";
  while(1)
  {
    usleep(1000000);
    int x = rand() % 10 + 1;
    int y = rand() % 11 + 1;
    char op = str_op[rand() %4];
    cout <<"生产者构造任务：" << x <<" " <<op  <<" " <<y <<"  = ?" <<endl; 
    Task task(x,y,op);
    bq->push(task);
  }
}

void* com(void* arg)
{
  BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(arg);
  while(1)
  {
    usleep(1000000);
    Task t;
    bq->pop(&t);
    cout <<"消费者开始处理任务......" ;
    t.Handle();
  }

}

int main()
{
  BlockQueue<Task> bq;
  pthread_t tid1,tid2; 
  pthread_create(&tid1,nullptr,pro,(void*)&bq);
  pthread_create(&tid2,nullptr,com,(void*)&bq);

  pthread_join(tid1,nullptr);
  pthread_join(tid2,nullptr);
  return 0;
}
